using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;
public class ParentScroll : MonoBehaviour
{

    // Use this for initialization
    ScrollRect scroll;
   public List<VHScrollRect> scrollList;
    public bool isScrolling;
    RectTransform contentRect;
    int count;
    float lastPos;
    float CurPos;
    public float CurProgress;
    public int CurView;
    public Action OnNext;
    public Action OnPrevior;
     int SwitchDistance=4;
    private void Awake()
    {
        scroll = GetComponent<ScrollRect>();
        scroll.onValueChanged.AddListener(OnScroll);
        contentRect = scroll.content;
        CurView = 0;
        scrollList = new List<VHScrollRect>(GetComponentsInChildren<VHScrollRect>());
    }
    void Start()
    {
       
        foreach (var item in scrollList)
        {
            item.isVertical = true;
            item.parentScroll = scroll;
            item.begeinDragDel = OnBeginDrag;
            item.endDragDel = OnEndDrag;
        }
        count = scrollList.Count;
        SetContent();
    }
    public Vector3 Pos;
    private void OnEndDrag()
    {
        float Wid = Input.mousePosition.x - Pos.x;
        if (Wid > 0)
        {
            if (Wid > scroll.viewport.sizeDelta.x / SwitchDistance)
            {
                if (CurView > 0)
                    CurView--;
                if (OnPrevior != null)
                    OnPrevior();
            }
        }
        else
        {
            if (-Wid > scroll.viewport.sizeDelta.x / SwitchDistance)
            {
                if (CurView < count - 1)
                    CurView++;
                if (OnNext != null)
                    OnNext();
            }
        }
        float target = CurView * 1f / (count - 1);
        DOTween.To(() => scroll.horizontalNormalizedPosition,
            (x) => scroll.horizontalNormalizedPosition = x, target,
            Mathf.Abs(target - scroll.horizontalNormalizedPosition));
    }

    private void OnBeginDrag()
    {
        Pos = Input.mousePosition;
    }

    private void OnScroll(Vector2 arg0)
    {
    }
    bool IsFinish;
    public void SetChildContent()
    {
        foreach (var item in scrollList)
        {
            item.SetContent();
        }
    }

    void SetContent()
    {
        Vector2 size = contentRect.sizeDelta;

        size.x = contentRect.GetComponent<GridLayoutGroup>().cellSize.x * count
              + contentRect.GetComponent<GridLayoutGroup>().spacing.x * (count + 1)
              + contentRect.GetComponent<GridLayoutGroup>().padding.left + contentRect.GetComponent<GridLayoutGroup>().padding.right;
        contentRect.sizeDelta = size;
    }
    // Update is called once per frame
    void Update()
    {
        CurProgress = scroll.horizontalNormalizedPosition;
    }
    public void SetCanScroll(bool can)
    {
        foreach (var item in scrollList)
        {
            item.CanScroll = can;
        }
    }
}
